from django.db import models
from stuff.models import Stuff, Weapon, Armor
from django.utils import timezone


def user_directory_path(instance, filename):
    return 'media/user/avatar/{}/{}'.format(instance.id, filename)


class User(models.Model):
    username = models.CharField(max_length=64, unique=True, verbose_name='用户名')
    nick_name = models.CharField(max_length=64, default='', verbose_name='昵称')
    avatar = models.ImageField(upload_to=user_directory_path, default='', verbose_name='头像')
    password = models.CharField(max_length=64, verbose_name='密码')
    phone = models.CharField(max_length=16, verbose_name='电话')
    email = models.EmailField(verbose_name='邮箱')
    date_joined = models.DateTimeField(auto_now_add=True, verbose_name='注册时间')
    last_login = models.DateTimeField(default=timezone.now, verbose_name='上次登录时间')

    class Meta:
        verbose_name = '用户信息'
        ordering = ['username']
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


class PlayerStatus(models.Model):
    player = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='玩家')
    level = models.SmallIntegerField(default=1, verbose_name='等级')
    location = models.CharField(max_length=32, default=['威尼斯', '酒馆'], verbose_name='玩家所在地')
    experience = models.SmallIntegerField(default=0, verbose_name='当前经验')
    max_experience = models.SmallIntegerField(default=100, verbose_name='当前等级最大经验')
    health = models.SmallIntegerField(default=100, verbose_name='当前生命值')
    max_health = models.SmallIntegerField(default=100, verbose_name='最大生命值')
    min_attack = models.SmallIntegerField(default=1, verbose_name='最小攻击')
    max_attack = models.SmallIntegerField(default=3, verbose_name='最大攻击')
    defense = models.SmallIntegerField(default=1, verbose_name='防御值')
    agility = models.SmallIntegerField(default=1, verbose_name='敏捷')
    momentum = models.SmallIntegerField(default=1, verbose_name='士气')

    class Meta:
        verbose_name = '玩家状态信息'
        ordering = ['player']
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.player.nick_name


class PlayerStuff(models.Model):
    player = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='玩家')
    stuff = models.ForeignKey(Stuff, on_delete=models.CASCADE, verbose_name='玩家背包物品')
    inventory = models.SmallIntegerField(default=1, verbose_name='物品数量')

    class Meta:
        verbose_name = '玩家物品栏信息'
        ordering = ['player']
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.player


class PlayerEquipment(models.Model):
    player = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='玩家')
    stuff = models.ForeignKey(Stuff, on_delete=models.CASCADE, verbose_name='玩家背包物品')

    class Meta:
        verbose_name = '玩家装备信息'
        ordering = ['player']
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.player
